﻿using UnityEngine;
using UnityEngine.UI;

[ExecuteInEditMode]
public class TextContainerHeight : MonoBehaviour
{
	public Text text;
	public float add;
	RectTransform rectTransform;

	void Update()
	{
		if (text)
		{
			if (Time.frameCount % 30 == 0)
			{
				if (!rectTransform) rectTransform = GetComponent<RectTransform>();
				Vector2 size = rectTransform.sizeDelta;
				size.y = GetHeight();
				rectTransform.sizeDelta = size;
			}
		}
	}

	/// <summary>
	/// 供外部调用（内部刷新频率不及时）
	/// </summary>
	public float GetHeight()
	{
		return text.preferredHeight + add;
	}
}